package com.zhao.dota.task;

import cn.hutool.core.bean.BeanUtil;
import com.zhao.dota.common.task.BaseTask;
import com.zhao.dota.model.Hero;
import com.zhao.dota.processor.HeroProcessor;
import com.zhao.dota.service.HeroService;
import com.zhao.dota.struct.SteamResult;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class UpdateHeroTask extends BaseTask {

    @Resource
    private HeroProcessor heroProcessor;
    @Resource
    private HeroService heroService;

    @Override
    public void execute() {
        SteamResult<HeroProcessor.Container> heroList = heroProcessor.getHeroList();
        List<Hero> heroes = heroList.getResult().getHeroes().stream().map(h -> {
            Hero hero = new Hero();
            BeanUtil.copyProperties(h, hero);
            return hero;
        }).collect(Collectors.toList());
        heroService.updateList(heroes);
    }

    @Override
    public String getKey() {
        return "heroTask";
    }

}
